#include <unistd.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "func_lcd.h"

//加载界面
void welcome(lcd p){
    //
    show_bmp(0,0,"./bmp/user/w.bmp");
    show_bmp(300,120,"./bmp/user/welcome.bmp");

    //进度条 x:0~800 y:382~432
    for(int i = 0;i<800;i++){
        for(int j = 381;j<432;j++){
            p[800*j+i] = 0x0000ff00; //绿色
        }

        usleep(10000); //等待10毫秒
    }
}
//检测坐标
int get_key(){
    //数字按键的起始坐标
    const int start_x = 456,start_y = 81;
    //数字按键的最终坐标
    const int end_x = 746,end_y = 390;
    //数字按键的x,y的大小(单位:5px)
    const int key_x = 17,key_y = 13;
    int key = -1;

    int x =-1,y=-1;
    get_xy(&x,&y);
    
    //检测鼠标是否在数字键盘范围之内
    if(x>=start_x && x<=end_x && y>=start_y && y<=end_y){
        //进一步检测
        int tmp_x = ((x-start_x)/5+1)%(key_x+4)+1;
        int tmp_y = ((y-start_y)/5+1)%(key_y+3)+1;
        //检测坐标是否点击到具体按键
        if(tmp_x<key_x && tmp_y<key_y){
            printf("yes!\n");
            tmp_x = ((x-start_x)/5+1)/(key_x+4);
            tmp_y = ((y-start_y)/5+1)/(key_y+3);
            key = tmp_x+tmp_y*3+1;
        }
    }
    return key;
}

//登录界面
int login(){
    show_bmp(0,0,"./bmp/user/login.bmp");
    //登录密码
    const char password[] = "12345"; 
    //输入框的起始坐标
    int input_x = 65,input_y = 222;
    //输入框单个数字在x轴的大小
    const int num_x = 60;
    int n = 0;
    char pw_in[] = "00000";
    //循环检测鼠标点击
    do{
        int key = get_key();
        printf("key = %d\n",key);
        if(key == -1){
            continue;
        }
        if(key == 12 && n==5 && strcmp(pw_in,password)==0){
            break;
        }
        else if(key == 10 && n>0){
            input_x-=num_x;
            pw_in[n] = 0;
            n--;
            //显示空白60*60
            show_bmp(input_x,input_y,"./bmp/user/point-white.bmp");
        }
        else if(key!=10 && key!=12 && n<strlen(password)){
            //追加原点图片
            show_bmp(input_x,input_y,"./bmp/user/point.bmp");
            input_x+=num_x;
            pw_in[n++] = key%12+'0';
        }
    }while(1);
    printf("密码正确!\n");
    return 1;
}

typedef struct 
{
    char name[32];
    int num;
    char pic[32];
    float pirce;
}Dish;


//菜品展示,返回是否可以左右翻页的标志
int dispaly(const Dish* dishs,int n,int page){

    show_bmp(0,0,"./bmp/user/background.bmp");
    //起始坐标
    int start_x = 200,start_y = 0;
    //每一个菜品图片所占用的空间
    int dish_x = 200,dish_y = 200;
    //操作图片占用的空间
    int number_x = 200,number_y = 40;
    for(int i = 4*(page-1);i<4*page&&i<n;i++){
        show_bmp(start_x,start_y,dishs[i].pic);
        show_bmp(start_x,start_y+dish_y,"./bmp/user/number.bmp");
        start_x+=dish_x;
        //换行
        if(start_x == 600){
            start_x = 200;
            start_y+=(dish_y+number_y);
        }
    }
    show_bmp(0,420,"./bmp/user/shop.bmp");
    int flag = 0;
    if(page == 1){
        show_bmp(700,400,"./bmp/user/right.bmp");
        flag = 1;
    }
    else if(page == (n/4+(n%4>0))){
        show_bmp(640,400,"./bmp/user/left.bmp");
        flag = -1;
    }
    else{
        show_bmp(640,400,"./bmp/user/move.bmp");
    }
    
    return flag;
}

//处理鼠标请求
int deal_request(Dish* dishs,int n,int page,int flag){
    //数组存储菜品的数量
    int count[n];
    memset(count,0,n*sizeof(int));
    int item = 0;
    //总金额
    float sum = 0;
    while(1){
        
        //加减按键
        int add_x = 200,add_y = 200;
        int sub_x = 350,sub_y = 200;
        int key_w = 50,key_h = 40;

        int x = -1,y = -1;
        get_xy(&x,&y);
        //如果翻页
        if(flag!=1 && x>640 && x<700 && y>400 && y<460){
            //可以左翻
            page--;
            flag = dispaly(dishs,n,page);
        }
        else if(flag!=-1 && x>700 && x<760 && y>400 && y<460){
            //可以右翻
            page++;
            flag = dispaly(dishs,n,page); 
        }
        //如果点击加减
        else if(x>200 && x<450 && y>200 && (x-add_x)%200<key_w && (y-add_y)%240<key_h){
            item = 4*(page-1)+2*(y-add_y)/240+(x-add_x)/200;
            count[item]++;
            sum+=dishs[item].pirce;
        }
        else if(x>350 && x<600 && y>200 && (x-sub_x)%200<key_w && (y-sub_y)%240<key_h){
            item = 4*(page-1)+2*(y-sub_y)/240+(x-sub_x)/200;
            if(count[item]>0){
                count[item]--;
                sum-=dishs[item].pirce;
            }
        }
        //如果点击结算
        else if(x<60 && y>420){
            //跳转支付页面
            show_bmp(0,0,"./bmp/user/weixin.bmp");
            show_bmp(0,0,"./bmp/user/back.bmp");
            show_bmp(380,440,"./bmp/user/s.bmp");
            char money[10] = "";
            sprintf(money,"%.2f",sum);
            int start_x = 10,start_y = 400;
            for(int i = 0;money[i]!='\0';i++,start_x+=40){
                if(money[i] == '.'){
                    show_bmp(start_x,start_y,"./bmp/total/point.bmp");
                    continue;
                }
                char tmp[] = "./bmp/total/0.bmp";
                tmp[12] = money[i];
                show_bmp(start_x,start_y,tmp);
                
            }
            while(1){
                x = -1;
                y = -1;
                get_xy(&x,&y);
                if(x<100 && y<60){
                    //点击返回
                    dispaly(dishs,n,page);
                    break;
                }
                if(x>380 && x<420 && y>440){
                    //点击退出
                    return 0;
                }
            } 
        }
        printf("当前购物车金额:%.2f\n",sum);
    }
}

int menu(){
    FILE* fp = fopen("./order.txt","r");
    if(fp == NULL){
        perror("fopen err");
        return -1;
    }
    //最多32道菜
    Dish dishs[32];
    memset(dishs,0,sizeof(dishs));
    int n = 0;
    for(int i = 0; ;i++){
        char tmp[128] = "";
        if(fgets(tmp,sizeof(tmp),fp)!=NULL){
            sscanf(tmp,"菜名:%s 数量:%d 价格:%f 图片:%s",dishs[i].name,&dishs[i].num,&dishs[i].pirce,dishs[i].pic);
            n++;
        }
        else{
            break;
        }
    }
    //默认显示第一页
    int page = 1;
    
    int flag = dispaly(dishs,n,page);
    deal_request(dishs,n,page,flag);
}
